package mt.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import mt.Order;

public class ViewOrderWindow extends JFrame {


	private static final long serialVersionUID = 8893248140171615478L;
	private JPanel panel;
	private JTextArea text;
	private JScrollPane textScrollPane ;

	/**
	 * The ViewOrderWindow represents a frame that shows the available orders of the client
	 *
	 * @author group 55
	 */

	public ViewOrderWindow(HashMap<Integer, Order> Orders){
		config();
		build(Orders);
		setVisible(true);
	}

	/**
	 * Procedure responsible for the layout  configuration
	 *
	 */
	private void config(){
		setLookNFeel();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.setBorder(new TitledBorder(new EtchedBorder (), "Buy/Sell Orders List" ));
		text = new JTextArea();
		text.setEditable(false);
	}

	/**
	 * Procedure responsible for the content (where the orders are shown) of the frame configuration
	 */
	private void build(HashMap <Integer, Order> orders){
		editText(orders);
		textScrollPane = new JScrollPane(text);
		textScrollPane.setPreferredSize(new Dimension(200, 200));
		textScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		textScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		panel.add(textScrollPane);
		add(panel);
		setPreferredSize(new Dimension(550,600));
		pack();
		setLocationRelativeTo(null);
	}

	/**
	 *
	 * procedure responsible for writing the order's information in the textArea
	 * @param orders
	 */
	private void editText(HashMap <Integer, Order> orders) {
		if(orders.size() < 1){
			text.setText("There is no orders available.");
		}
		else{
			for (Order order : orders.values())
				text.append(order.toString()+"\n");
		}
	}

	/**
	 * Set Look & Feel
	 */
	private void setLookNFeel(){
		try {

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (UnsupportedLookAndFeelException e) {}
		catch (ClassNotFoundException e) {}
		catch (InstantiationException e) {}
		catch (IllegalAccessException e) {}
	}
}
